<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-input
          v-model="params.enum_key"
          clearable
          @keyup.enter.native="getData()"
          placeholder="类型值"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.enum_value"
          clearable
          @keyup.enter.native="getData()"
          placeholder="类型名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getData()" icon="el-icon-search" :loading="loading">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="editData()" icon="el-icon-plus" :loading="loading">添加</el-button>
      </el-form-item>
    </el-form>
    <c-table
      border
      class="table-sm"
      v-loading="loading"
      :columns="tableColumns"
      :data="tableData"
    ></c-table>
    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <edit-bank-type
      :visible.sync="editVisible"
      :form-data="formData"
      :max-key="maxKey"
      @success="getData()"
    ></edit-bank-type>
  </div>
</template>

<script>
import EditBankType from '@/cadmin/views/org/components/bank/EditBankType'
export default {
  components: {
    EditBankType,
  },
  props: {},
  data() {
    return {
      params: {
        index: 1,
        size: 10,
        'enum_field.eq': 'credit_agency_info.agency_type_sub',
      },
      order: {
        order_num: 'desc',
      },
      tableName: 'system_enum',
      loading: false,
      tableData: [],
      totalCount: 0,
      tableColumns: [
        { attrs: { label: '类型值', prop: 'enum_key' } },
        { attrs: { label: '类型名称', prop: 'enum_value' } },
        {
          attrs: { label: '操作', align: 'center' },
          operations: [
            {
              content: '修改信息',
              icon: 'fa fa-edit',
              event: this.editData,
            },
          ],
        },
      ],
      editVisible: false,
      formData: {},
      maxKey: '',
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getData()
  },
  mounted() {},
  methods: {
    editData(row) {
      this.editVisible = true
      this.maxKey = this.tableData?.[0]?.enum_key ?? ''
      this.formData = row ? { ...row } : {}
    },
    dataAdapter(item) {
      return item
    },
    getData() {
      this.loading = true
      this.$api
        .getData('credit', this.tableName, this.params, this.order)
        .then(res => {
          const data = (res && res.data) || {}
          this.tableData = (data.data || []).map(this.dataAdapter)
          this.totalCount = data.totalCount
        })
        .finally(() => {
          this.loading = false
        })
    },
  },
}
</script>

<style lang="scss" scoped></style>
